package com.xkdx.caipiao.hall;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Random;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.TextUtils;
import android.text.style.ForegroundColorSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.xkdx.caipiao.R;
import com.xkdx.caipiao.ThreadActivity;
import com.xkdx.caipiao.listener.OnAlertDialogOkListener;
import com.xkdx.caipiao.module.network.AbsModule;
import com.xkdx.caipiao.my.OrderPayActivity;
import com.xkdx.caipiao.my.TouZhuProtocolActivtiy;
import com.xkdx.caipiao.pubclass.SelectCode;
import com.xkdx.caipiao.statics.IConstants;
import com.xkdx.caipiao.util.CaipiaoUtil;
import com.xkdx.caipiao.wiget.MyAlertDialog;
import com.xkdx.caipiao.wiget.MyToast;
import com.xkdx.guangguang.util.SharePrefenceUtil;

public class BetSevenActivity extends ThreadActivity implements
		OnAlertDialogOkListener {

	private View bet;
	private Button btn_bet;
	private TextView title_tv, clearNumbers, mutiply_tv, clear;
	private Button addSelf, addRandom; // 自选号码，机选号码
	private LinearLayout numbersListView;
	private LayoutInflater inflater;
	private String betting;
	int baiwanNum, shiwanNum, wanNum, qianNum, baiNum, shiNum, geNum;

	private List<String> baiwanLs = new ArrayList<String>();
	private List<String> shiwanLs = new ArrayList<String>();
	private List<String> wanLs = new ArrayList<String>();
	private List<String> qianLs = new ArrayList<String>();
	private List<String> baiLs = new ArrayList<String>();
	private List<String> shiLs = new ArrayList<String>();
	private List<String> geLs = new ArrayList<String>();

	int betingNum = 0;
	int money = 0;
	SharePrefenceUtil sp;
	TextView protocal;
	
	Button add, sub;
	TextView tv_beishu;
	int beishu = 1;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.bet_caipiao);
		sp = new SharePrefenceUtil(getApplicationContext(),
				SharePrefenceUtil.USERINFO);
		findView();
		setOnClick();
		setInfo();
	}

	private void setInfo() {
		numbersListView.removeAllViews();
		int num = IConstants.list_sevencolor.size();
		for (int i = num; i > 0; i--) {
			addNumbersView(i - 1);
		}

		String text = getBetInfo();
		mutiply_tv.setText(text);
	}

	/**
	 * 将当期所有投注信息汇总，*注*元 String
	 */
	private String getBetInfo() {
		betingNum = 0;
		money = 0;

		int count = IConstants.list_sevencolor.size();
		for (int i = count; i > 0; i--) {
			betingNum += Integer.parseInt(IConstants.list_sevencolor.get(i - 1)
					.getBettingNum());
			// money += Integer.parseInt(IConstants.list_sevencolor.get(i - 1)
			// .getMoneyPerBet());
		}

		money = betingNum * beishu*2;
		StringBuilder sb = new StringBuilder();
		if (betingNum != 0) {
			sb.append(betingNum + "注X" + beishu + "倍=" + money + "元");
		} else {
			sb.setLength(0);
			sb.append("");
		}

		return sb.toString();
	}

	private void setOnClick() {
		addSelf.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				Intent intent = new Intent(getApplicationContext(),
						SevenActivity.class);
				startActivity(intent);
				finish();
			}
		});
		addRandom.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				produceNumbers();
				getCaiPiaoObject(baiwanLs, shiwanLs, wanLs, qianLs, baiLs,
						shiLs, geLs);
				numbersListView.removeAllViews();
				int num = IConstants.list_sevencolor.size();
				for (int i = num; i > 0; i--) {
					addNumbersView(i - 1);
				}
				String text = getBetInfo();
				mutiply_tv.setText(text);
			}
		});

		clearNumbers.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				numbersListView.removeAllViews();
				IConstants.list_sevencolor.clear();
				mutiply_tv.setText("");
			}
		});

	}

	private void findView() {
		inflater = LayoutInflater.from(this);
		numbersListView = (LinearLayout) findViewById(R.id.bet_numbers);
		clearNumbers = (TextView) findViewById(R.id.clear_numbers);
		addSelf = (Button) findViewById(R.id.add_self_number);
		addRandom = (Button) findViewById(R.id.add_random_number);
		protocal = (TextView) findViewById(R.id.textView3);
		add = (Button) findViewById(R.id.btn_add);
		sub = (Button) findViewById(R.id.btn_sub);
		tv_beishu = (TextView) findViewById(R.id.tv_num);
		SpannableStringBuilder style = new SpannableStringBuilder(getResources().getString(R.string.bet_rule_tip));
		style.setSpan(new ForegroundColorSpan(Color.BLUE), 5, style.length(),
				Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
		protocal.setText(style);
		protocal.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				Intent intent = new Intent(BetSevenActivity.this,
						TouZhuProtocolActivtiy.class);
				startActivity(intent);
			}

		});
		title_tv = (TextView) findViewById(R.id.pay_title);
		title_tv.setText(getResources().getString(R.string.seven_color));
		bet = findViewById(R.id.bet_pay);
		btn_bet = (Button) bet.findViewById(R.id.bet);
		mutiply_tv = (TextView) bet.findViewById(R.id.mutiply_tv);
		clear = (TextView) bet.findViewById(R.id.clear);
		btn_bet.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				if (IConstants.list_sevencolor.size() > 0) {
					if (!(TextUtils.isEmpty(usrSP.getLogintoken()))) {
						ConvertIntent();

					} else {
						setLogin(100);
					}
				} else {
					MyToast.makeText(getApplicationContext(), "请至少投一注...", 0)
							.show();
				}
			}
		});
		btn_bet.setText("付款");
		clear.setVisibility(View.GONE);
		
		add.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				beishu++;
				tv_beishu.setText(beishu + "");
				String text = getBetInfo();
				mutiply_tv.setText(text);
			}
		});
		sub.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (beishu > 1) {
					beishu--;

				}
				tv_beishu.setText(beishu + "");
				String text = getBetInfo();
				mutiply_tv.setText(text);

			}
		});

	}

	protected void ConvertIntent() {
		Intent pay = new Intent(BetSevenActivity.this, OrderPayActivity.class);
		betting = CaipiaoUtil.getCaiPiaoString(IConstants.list_sevencolor);
		pay.putExtra("betting", betting);
		pay.putExtra("money", money+"");
		pay.putExtra("beishu", beishu+"");
		pay.putExtra("lotteryid", "qxc");
		startActivity(pay);
	}

	private void setnumbers(final View view, final int i) {
		TextView rednumbers = (TextView) view.findViewById(R.id.rednumbers);
		TextView bluenumbers = (TextView) view.findViewById(R.id.bluenumbers);

		bluenumbers.setVisibility(View.GONE);
		TextView tip = (TextView) view.findViewById(R.id.tip);
		rednumbers.setText(IConstants.list_sevencolor.get(i).getRedCode());
		tip.setText(IConstants.list_sevencolor.get(i).getBetMode());
		ImageView im_del = (ImageView) view.findViewById(R.id.delete);
		final SelectCode code = IConstants.list_sevencolor.get(i);
		im_del.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				numbersListView.removeView(view);
				IConstants.list_sevencolor.remove(code);
				String text = getBetInfo();
				mutiply_tv.setText(text);
			}
		});

	}

	public void onBackClick(View view) {
		exit();
	}

	private void exit() {
		if (IConstants.list_sevencolor.size() == 0) {
//			Intent intent=new Intent(this,MainTabActivity.class);
//			startActivity(intent);
			finish();
		} else {
			MyAlertDialog.creatAlertDialog(BetSevenActivity.this, "温馨提示",
					"是否消除本次选号?");
			MyAlertDialog.setOnAlertDialogOklistener(this);
			MyAlertDialog.setOkMessage("保存");
			MyAlertDialog.setDismissMessage("清除");
		}
	}

	@Override
	public void onBackPressed() {

		exit();
	}

	public void onBetClick(View view) {

	}

	class Adatper extends BaseAdapter {
		viewHolder holder = null;

		@Override
		public int getCount() {
			return 0;
		}

		@Override
		public Object getItem(int arg0) {
			return null;
		}

		@Override
		public long getItemId(int arg0) {
			return 0;
		}

		@Override
		public View getView(int position, View contentView, ViewGroup arg2) {
			viewHolder holder = null;
			if (contentView == null) {
				holder = new viewHolder();
				contentView = inflater.inflate(R.layout.pay_number_item_caipiao, null);
				holder.rednumbers = (TextView) contentView
						.findViewById(R.id.rednumbers);
				holder.tip = (TextView) contentView.findViewById(R.id.tip);
			} else {
				holder = (viewHolder) contentView.getTag();
			}

			return contentView;
		}

		class viewHolder {
			TextView rednumbers, bluenumbers, tip;
		}

	}

	private void addNumbersView(int i) {
		View numbesView = inflater.inflate(R.layout.pay_number_item_caipiao, null);
		setnumbers(numbesView, i);
		numbersListView.addView(numbesView);
	}

	@Override
	public void showOnPost(HashMap<String, AbsModule> result) {

	}

	@Override
	public void startThread() {

	}

	private void produceNumbers() {
		clearNumbersList();

		Random random = new Random();

		baiwanNum = random.nextInt(9) + 1;
		shiwanNum = random.nextInt(9) + 1;
		wanNum = random.nextInt(9) + 1;
		qianNum = random.nextInt(9) + 1;
		baiNum = random.nextInt(9) + 1;
		shiNum = random.nextInt(9) + 1;
		geNum = random.nextInt(9) + 1;

		baiwanLs.add(String.valueOf(baiwanNum));
		shiwanLs.add(String.valueOf(shiwanNum));
		wanLs.add(String.valueOf(wanNum));
		qianLs.add(String.valueOf(qianNum));
		baiLs.add(String.valueOf(baiNum));
		shiLs.add(String.valueOf(shiNum));
		geLs.add(String.valueOf(geNum));

	}

	private void clearNumbersList() {
		baiwanLs.clear();
		shiwanLs.clear();
		wanLs.clear();
		qianLs.clear();
		baiLs.clear();
		shiLs.clear();
		geLs.clear();
	}

	private void getCaiPiaoObject(List<String> baiwans, List<String> shiwans,
			List<String> wans, List<String> qians, List<String> bais,
			List<String> shis, List<String> ges) {
		StringBuffer sb = new StringBuffer();

		SelectCode code = new SelectCode();

		for (String baiwan : baiwans) {

			sb.append(baiwan);
		}

		sb.append(" ");
		for (String shiwan : shiwans) {

			sb.append(shiwan);
		}

		sb.append(" ");

		for (String wan : wans) {

			sb.append(wan);
		}

		sb.append(" ");
		for (String qian : qians) {

			sb.append(qian);
		}

		sb.append(" ");

		for (String bai : bais) {

			sb.append(bai);
		}

		sb.append(" ");
		for (String shi : shis) {

			sb.append(shi);
		}

		sb.append(" ");
		for (String ge : ges) {

			sb.append(ge);
		}

		code.setRedCode(sb.toString());

		code.setBlueCode(sb.toString());
		code.setBetMode(getString(R.string.pay_numbers_tip));
		code.setBettingNum("1");
		code.setMoneyPerBet("2");
		code.setCaizhong(getResources().getString(R.string.arrange_5));
		IConstants.list_sevencolor.add(code);

	}

	@Override
	public void handleOkClick() {
		MyToast.makeText(getApplicationContext(), "已保存，下次进入彩种时直接调出", 0).show();
//		Intent intent=new Intent(this,MainTabActivity.class);
//		startActivity(intent);
		finish();
	}

	@Override
	public void handleDismissClick() {
		IConstants.list_sevencolor.clear();
//		Intent intent=new Intent(this,MainTabActivity.class);
//		startActivity(intent);
		finish();
	}

	@Override
	protected void onActivityResult(int arg0, int resultCode, Intent arg2) {
		if (resultCode == 200) {

			ConvertIntent();
		}
		super.onActivityResult(arg0, resultCode, arg2);
	}

}
